package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv
import com.library.common.toast.XToast
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentShopHomeLiveBinding
import com.vitabuybuy.tv.dto.LiveListDto
import com.vitabuybuy.tv.dto.ReCallBackBean
import com.vitabuybuy.tv.ui.shopcenter.store.viewmodel.StoreHomeViewModel
import com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.commondef.RoomInfo
import com.vitabuybuy.tv.ui.tencent.liveroom.ui.AudienceLiveRoomActivity
import com.vitabuybuy.tv.ui.tencent.superplay.SuperPlayVidelMainActivity
import kotlinx.android.synthetic.main.fragment_shop_home_live.*

class StoreHomeLiveFragment : BaseFragment<StoreHomeViewModel, FragmentShopHomeLiveBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_shop_home_live

    private var storeId: Int? = -1
    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
    }

    override fun lazyLoadData() {
        storeId = (parentFragment as StoreHomeFragment).getStoreId()
        viewModel.getLiveList(storeId, pageNumber, 10)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getLiveList(storeId, pageNumber, 10)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getLiveList(storeId, pageNumber, 10)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getLiveList(storeId, pageNumber, 10)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                val item = (msg.obj as LiveListDto)
                val storeDetail = (parentFragment as? StoreHomeFragment)?.getStoreDetail()
                if (item.status == "已结束") {
                    if (item.replayAddress == null) {
                        XToast.normal(getString(R.string.play_link_is_null))
                        return
                    }
                    val bean = ReCallBackBean(
                        playUrl = /**if(item.replayAddress != null)*/ item.replayAddress /**else "https://v-cdn.zjol.com.cn/276984.mp4"*/,
                        mCoverUrl = item.image,
                        mTitle = item.title,
                        audienceCount = item.viewerQuantity,
                        authorName = item.store?.name,
                        authorId = item.store?.id.toString(),
                        authorRegion = item.allRegion,
                        authorAvatar = item.store?.logo,
                        attention = item.isAttention,
                    )
                    LiveReCallBackActivity.openActivity(mContext, bean)
                } else {
                    val roomInfo = RoomInfo().apply {
                        audienceCount = item.viewerQuantity
                        roomID = item.roomNumber
                        roomInfo = item.title
                        roomName = item.title
                        roomCreator = item.store?.id.toString()
                        mixedPlayURL = item.playUrl
                        authorName = item.store?.name
                        authorId = item.store?.id.toString()
                        authorRegion = item.allRegion
                        authorAvatar = item.store?.logo
                        liveId = item.id
                        attention = item.isAttention
                        userId = mmkv.decodeInt(MMKVContancts.USER_ID).toString()
                        userName = mmkv.decodeString(MMKVContancts.USER_NICKNAME)
                        createRoom = false
                    }
                    AudienceLiveRoomActivity.openActivity(mContext, roomInfo)
                }
            }
        }
    }
}
